//
//  NextViewController.m
//  B段项目
//
//  Created by 昶博 on 15/9/24.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "NextViewController.h"
#import "NextCell.h"
#import "TitleView.h"
#import <AFNetworking.h>
#import "DetailViewController.h"
#import "BelowView.h"
#import <UIImageView+WebCache.h>
#import "Player.h"

@interface NextViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic ,strong)TitleView *titleView;

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"%ld",self.model.album_id);
    [self  tabviewSubView];
   [self dataHandle];
    [self.tableView registerClass:[NextCell class] forCellReuseIdentifier:@"reuse"];
    
    
    self.belowView = [[BelowView alloc]initWithFrame:CGRectMake(0, ScreenHeight-150*h, ScreenWidth, 200*h)];
    self.belowView.backgroundColor =[UIColor colorWithRed:0.027 green:0.212 blue:0.278 alpha:0.5];
    
    
    self.tableView.bounces = NO;
  
    [self.view addSubview:self.belowView];
    
    
}

-(void)tabviewSubView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-68*h) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
}
-(void)dataHandle
{
    self.arr = [NSMutableArray array];
    NSString *Url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.9.5/News/newsonewradiolist-pm1-aid%ld-p1-s50-tp0.json",self.model.album_id ];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    //设置安全级别
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    [manager GET:Url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arr = responseObject[@"result"][@"list"];
      
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
   NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    ForthModel *model = [[ForthModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    
    
    cell.selectionStyle = NO;
    cell.model = model;
    return cell;
    
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  
 TitleView *title = [[TitleView alloc]initWithFrame:CGRectMake(0, 64 * h, self.view.frame.size.width, 200 * h)];
   
    title.model = self.model;
    return title;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90 * h;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200 * h;
     
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.belowView.imageView sd_setImageWithURL:[NSURL URLWithString:self.model.album_imgurl] placeholderImage:[UIImage imageNamed:@"44"]];
    ForthModel *model = [[ForthModel alloc]init];
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    [model setValuesForKeysWithDictionary:dic];
    self.belowView.titleLabel.text = model.program_title;
    self.belowView.label.text = model.program_live_time;
    
    [Player share].Pic = self.model.album_imgurl;
    [Player share].BigPic = self.model.album_bigImgurl;
    [Player share].titleLabel = model.program_title;
    [Player share].label = model.program_live_time;
    [Player share].model = model;
     
    [[Player share] playWithUrl:[NSURL URLWithString:model.program_fileurl]];
    [Player share].str = NO;
  
    [self.belowView.button  setBackgroundImage:[UIImage imageNamed:@"zantibai"] forState:UIControlStateNormal];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"changeImage" object:self.model];
   
    NSNotificationCenter *center1 = [NSNotificationCenter defaultCenter];
    [center1 postNotificationName:@"changeLabel" object:model userInfo:nil];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
